package com.algo.pattern.observable.schedule;

import com.algo.pattern.observable.model.StockUpdateMessage;
import com.algo.pattern.observable.observable.StockUpdateObserverManager;
import com.algo.pattern.observable.queue.StockUpdateQueueManager;
import com.algo.pattern.observable.queue.StockUpdateQueueManagerImpl;

/**
 * 模拟调度中心定时任务
 */
public class ScheduleServiceTask extends Thread {

    private StockUpdateQueueManager queueManager = StockUpdateQueueManagerImpl.getInstance();

    private StockUpdateObserverManager observerManager = StockUpdateObserverManager.getInstance();

    @Override
    public void run() {
        while(true) {
            boolean updateResult = false;
            StockUpdateMessage message = null;
            try {
                message = queueManager.take();
                System.out.println("调度中心消费到消息，message：" + message);
                // 更新本地数据库数据
                // 。。。。。。。。代码省略
                Thread.sleep(500);

                observerManager.inform(message.getMessageId(), true);
            } catch (Exception e) {
                if (message != null) {
                    observerManager.inform(message.getMessageId(), false);
                }
            }
        }
    }
}
